package com.ruoyi.project.manage.item.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.manage.approveLog.domain.ApproveLog;
import com.ruoyi.project.manage.approveLog.service.IApproveLogService;
import com.ruoyi.project.manage.file.domain.ProjectFile;
import com.ruoyi.project.manage.file.service.IProjectFileService;
import com.ruoyi.project.manage.item.domain.ProjectItem;
import com.ruoyi.project.manage.item.service.IProjectItemService;
import com.ruoyi.project.manage.project.domain.Project;
import com.ruoyi.project.manage.project.service.IProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目事项Controller
 * 
 * @author fangkun
 * @date 2022-04-02
 */
@Controller
@RequestMapping("/manage/itemdetail")
public class ProjectItemDetailController extends BaseController
{
    private String prefix = "manage/item";

    @Autowired
    private IProjectItemService projectItemService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private IApproveLogService approveLogService;
    @Autowired
    private IProjectFileService projectFileService;

    @RequiresPermissions("manage:item:view")
    @GetMapping()
    public String item()
    {
        return prefix + "/item";
    }

    /**
     * 查询项目事项列表
     */
    @RequiresPermissions("manage:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Project project)
    {
        startPage();
        project.setProjectStatus(ProjectConstants.APPROVAL_AGREE);
        // 设置当前登录人查看本人录入的项目信息
        project.setCreateBy(ShiroUtils.getLoginName());
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 修改项目信息
     */
    @RequiresPermissions("manage:items:edit")
    @GetMapping("/manage/{projectId}")
    public String manage(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        // 获取项目信息
        Project project = projectService.selectProjectByProjectId(projectId);
        mmap.put("project", project);

        // 获取项目关联事项
        ProjectItem projectItem = new ProjectItem();
        projectItem.setProjectId(projectId);
        projectItem.setStatus(UserConstants.NORMAL);
        List<ProjectItem> projectItems = projectItemService.selectProjectItemList(projectItem);
        mmap.put("projectItems", projectItems);

        return prefix + "/edit";
    }

    /**
     * 项目事项一张图
     */
    @RequiresPermissions("manage:items:edit")
    @GetMapping("/map/{projectId}")
    public String map(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        // 获取项目信息
        Project project = projectService.selectProjectByProjectId(projectId);
        mmap.put("project", project);

        // 获取项目关联事项
        ProjectItem projectItem = new ProjectItem();
        projectItem.setProjectId(projectId);
        projectItem.setStatus(UserConstants.NORMAL);
        projectItem.setIsClose(ProjectConstants.YES);
        List<ProjectItem> projectItems = projectItemService.selectProjectItemList(projectItem);
        mmap.put("projectItems", projectItems);

        return prefix + "/detailMap";
    }

    /**
     * 项目事项管理
     */
    @RequiresPermissions("manage:items:edit")
    @GetMapping("/manageItem/{projectItemId}")
    public String manageItem(@PathVariable("projectItemId") Long projectItemId, ModelMap mmap)
    {
        // 获取项目事项信息
        ProjectItem projectItem = projectItemService.selectProjectItemByProjectItemId(projectItemId);
        mmap.put("projectItem", projectItem);

        // 获取项目事项流转
        // 获取流转信息
        ApproveLog param = new ApproveLog();
        param.setEntityId(projectItemId);
        param.setEntityType(ProjectConstants.APPROVAL_TYPE_ITEM);
        List<ApproveLog> approveLogs = approveLogService.selectApproveLogList(param);
        mmap.put("approveLogs", approveLogs);

        return prefix + "/detailManage";
    }

    /**
     * 项目事项查看
     */
    @RequiresPermissions("manage:items:edit")
    @GetMapping("/viewItem/{projectItemId}")
    public String viewItem(@PathVariable("projectItemId") Long projectItemId, ModelMap mmap)
    {
        // 获取项目事项信息
        ProjectItem projectItem = projectItemService.selectProjectItemByProjectItemId(projectItemId);
        mmap.put("projectItem", projectItem);

        // 获取项目事项流转
        // 获取流转信息
        ApproveLog param = new ApproveLog();
        param.setEntityId(projectItemId);
        param.setEntityType(ProjectConstants.APPROVAL_TYPE_ITEM);
        List<ApproveLog> approveLogs = approveLogService.selectApproveLogList(param);
        mmap.put("approveLogs", approveLogs);

        // 获取文件信息
        ProjectFile projectFile = new ProjectFile();
        projectFile.setProjectItemId(projectItemId);
        projectFile.setStatus(ProjectConstants.YES + "");
        List<ProjectFile> projectFiles = projectFileService.selectProjectFileList(projectFile);
        mmap.put("projectFiles", projectFiles);

        return prefix + "/detailView";
    }

    /**
     * 导出项目事项列表
     */
    @RequiresPermissions("manage:item:export")
    @Log(title = "项目事项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectItem projectItem)
    {
        List<ProjectItem> list = projectItemService.selectProjectItemList(projectItem);
        ExcelUtil<ProjectItem> util = new ExcelUtil<ProjectItem>(ProjectItem.class);
        return util.exportExcel(list, "项目事项数据");
    }

    /**
     * 新增项目事项
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目事项
     */
    @RequiresPermissions("manage:item:add")
    @Log(title = "项目事项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectItem projectItem)
    {
        return toAjax(projectItemService.insertProjectItem(projectItem));
    }

    /**
     * 修改项目事项
     */
    @RequiresPermissions("manage:item:edit")
    @GetMapping("/edit/{projectItemId}")
    public String edit(@PathVariable("projectItemId") Long projectItemId, ModelMap mmap)
    {
        ProjectItem projectItem = projectItemService.selectProjectItemByProjectItemId(projectItemId);
        mmap.put("projectItem", projectItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目事项
     */
//    @RequiresPermissions("manage:item:edit")
    @Log(title = "项目事项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectItem projectItem)
    {
        System.out.println(projectItem.getFileList());
        return toAjax(projectItemService.manageProjectItem(projectItem));
    }

    /**
     * 删除项目事项
     */
    @RequiresPermissions("manage:item:remove")
    @Log(title = "项目事项", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(projectItemService.deleteProjectItemByProjectItemIds(ids));
    }

    /**
     * 项目事项办理状态变更
     */
    @Log(title = "项目事项", businessType = BusinessType.UPDATE)
    @PostMapping( "/changeClose")
    @ResponseBody
    public AjaxResult changeClose(ProjectItem projectItem)
    {
        return toAjax(projectItemService.changeClose(projectItem));
    }
}
